﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Bamboo.Core.Common.Reflection;
using TinyERP.Framework.Plugin;
using TinyERP.Framework.Config;
using TinyERP.Core.DTO;
using TinyERP.Framework;
using TinyERP.Framework.Controls.Docking;
using TinyERP.Framework.Controls;

namespace TinyERP.Core.View.Controls
{
    public class TabContentPane : DockPanel, IContentPane
    {
        public event ContentPaneSelectedHandler OnSelectedPane;
        public new event EventHandler OnLostFocus;
        private IList<Form> openingForms;
        private IForm selectedForm;
        public TabContentPane()
            : base()
        {

            openingForms = new List<Form>();
            this.LostFocus += new EventHandler(OnTabContentPaneLostFocus);
            
        }

        private void OnTabContentPaneLostFocus(object sender, EventArgs e)
        {
            if (OnLostFocus != null)
            {
                OnLostFocus(this, e);
            }
        }
       

        public IForm GetCurrentForm()
        {
            if (this.selectedForm != null)
            {
                return this.selectedForm as IForm;
            }
            return null;
        }

       
        

        public void ShowPane(UINodeDTO formNode)
        {
            Form form = FindFormByUINode(formNode);
            if (form != null)
            {
                ((DockContent)form).Show();
            }
            else
            {
                AddTabFromNode(formNode);
            }
        }
        public void CloseAllPanes()
        {
            while (openingForms.Count > 0)
            {
                openingForms[0].Close();
            }
        }
        private void AddTabFromNode(UINodeDTO formNode)
        {
            Form form = PluginManager.GetInstance().CreateForm(formNode.UIClassName);
            if (form != null)
            {
                form.Tag = formNode;
                form.FormClosed += new FormClosedEventHandler(OnFormClosed);
                form.Enter+=new EventHandler(OnFormEnter); 
                
                ((IForm)form).ApplyACL(formNode.AllowEdit, formNode.AllowDelete, formNode.AllowCreate);
                
                string tabTitle = UIManager.GetInstance().GetTitleOfUINode(formNode);
                ((DockContent)form).Text = tabTitle;
                if (this.DocumentStyle == DocumentStyle.SystemMdi)
                {
                    ((DockContent)form).MdiParent = this.FindForm();
                    ((DockContent)form).Show();
                }
                else
                {
                    ((DockContent)form).Show(this, DockState.Document);
                }
                this.ResumeLayout(true, true);
                openingForms.Add(form);
            }
        }

        private void OnFormEnter(object sender, EventArgs e)
        {
            this.selectedForm = (IForm)sender;
            UINodeDTO selectedNode =  ((Form)sender).Tag as UINodeDTO;
            if (OnSelectedPane != null)
            {
                OnSelectedPane(this.selectedForm, selectedNode);
            }
            this.selectedForm.RegisterShortcuts();
            this.ResumeLayout(true, true);
        }

       

        private void OnFormClosed(object sender, FormClosedEventArgs e)
        {
            Form form = (Form)sender;
            openingForms.Remove(form);
        }


        private Form FindFormByUINode(UINodeDTO formNode)
        {
            foreach (Form form in this.openingForms)
            {
                if (form.Tag as UINodeDTO == formNode)
                {
                    return form;
                }
            }
            return null;
        }

       
        
    }
}
